﻿using System;
using System.Linq;
using System.Web.Mvc;
using Dentiste.UI.Models;

namespace Dentiste.UI.Controllers
{
    public class PrintController : Controller
    {
        //
        // GET: /Print/

        public ActionResult Index()
        {
            var id = Convert.ToInt32(RouteData.Values["id"] as string);
            var invoice = Domain.DentisteManager.Instance.SelectInvoice(id);
            if (invoice == null)
            {
                throw new Exception("Aucune facture associée à l'instance d'impression");
            }
            if (invoice.Patient == null)
            {
                throw new Exception("Aucun patient associé à cette facture");
            }
            var viewmodel = new PrintViewModel
                {
                    FirstName = invoice.Patient.FirstName,
                    LastName = invoice.Patient.LastName,
                    Rows = invoice.Rows
                };
            if (string.IsNullOrEmpty(Request.QueryString["printtype"]) || (!string.IsNullOrEmpty(Request.QueryString["printtype"]) && Request.QueryString["printtype"]=="none"))
            {
                // normal
                viewmodel.PrintType = PrintType.Normal;
            }
            else if (Request.QueryString["printtype"] == "vipo")
            {
                // VIPO
                viewmodel.PrintType = PrintType.Vipo;
            }
            else
            {
                throw new Exception("Le mode d'impression est inconnu");
            }
            return View(viewmodel);
        }
    }
}